package com.ruoyi.car.controller.admin.company;

import com.github.pagehelper.PageInfo;
import com.ruoyi.car.controller.admin.company.vo.CarCompanyAddReq;
import com.ruoyi.car.controller.admin.company.vo.CarCompanyEditReq;
import com.ruoyi.car.controller.admin.company.vo.CarCompanyReq;
import com.ruoyi.car.controller.admin.company.vo.CarCompanyResp;
import com.ruoyi.car.controller.admin.model.vo.CarModelAddReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelEditReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelResp;
import com.ruoyi.car.entity.CarCompany;
import com.ruoyi.car.entity.CarModel;
import com.ruoyi.car.mapstruct.CarCompanyConvert;
import com.ruoyi.car.mapstruct.CarModelConvert;
import com.ruoyi.car.service.CarCompanyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/6/6 15:08
 * @Version 1.0
 */
@Api(tags = "后台管理-客户公司信息")
@RestController
@RequestMapping(value = "/car/company")
public class CompanyController {

    @Autowired
    private CarCompanyService carCompanyService;

    @PreAuthorize(value = "@ss.hasPermi('info:company:page')")
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/page")
    public TableDataInfo search(CarCompanyReq req){
        PageInfo<CarCompany> pageInfo= this.carCompanyService.search(req);

        //总记录数
        long count= pageInfo.getTotal();
        //转换完成以后的结果
        List<CarCompanyResp> list= CarCompanyConvert.INSTANCE.convert(pageInfo.getList());
        TableDataInfo tableDataInfo=new TableDataInfo(list,count);
        return tableDataInfo;

    }

    @Log(title = "大客户公司信息",businessType = BusinessType.INSERT)
    @PreAuthorize(value = "@ss.hasPermi('info:company:add')")
    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Valid CarCompanyAddReq req){
        CarCompany convert = CarCompanyConvert.INSTANCE.convert(req);
        boolean save = this.carCompanyService.save(convert);
        return save? R.ok(): R.fail();
    }

    @PreAuthorize(value = "@ss.hasPermi('info:company:query')")
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/{id}")
    public R<CarCompanyResp> findById(@PathVariable(value = "id") Long id){
        CarCompany db = this.carCompanyService.getById(id);
        if(db==null){
            //国际化 i18n 中查询
            String msg= MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarCompanyResp convert = CarCompanyConvert.INSTANCE.convert(db);
        return R.ok(convert);
    }

    @Log(title = "大客户公司信息",businessType = BusinessType.UPDATE)
    @PreAuthorize(value = "@ss.hasPermi('info:company:edit')")
    @ApiOperation(value = "修改保存")
    @PutMapping
    public R  update(@RequestBody @Valid CarCompanyEditReq req){
        CarCompany db = this.carCompanyService.getById(req.getId());
        if(db==null){
            //国际化 i18n 中查询
            String msg=MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarCompany convert = CarCompanyConvert.INSTANCE.convert(req);

        boolean save = this.carCompanyService.updateById(convert);
        return save? R.ok(): R.fail();
    }
}
